package com.formula.datastructure.exam.sort;

import com.formula.datastructure.util.DataUtil;

public class BubbleSort {
    public Integer[] sort(Integer[] array) {
        int N = array.length;
        Integer temp;
        int flag = 1;
        for (int i = 0; i < N; i++) {
            if (flag == 0) {
                break;
            }
            flag = 0;  // flag的值不要搞错了
            for (int j = 0; j < N - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    flag = 1;
                    temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                }
            }
        }
        return array;
    }

    public static void main(String[] args) {
        BubbleSort bubble = new BubbleSort();

        int size = 20;
        int range = 999;
        Integer[] array = DataUtil.genUniqueArray(size, range);
        DataUtil.printIndex(size);
        DataUtil.printArray(array);
        DataUtil.printArray(bubble.sort(array));
    }
}
